<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="like">like</button>
    <script>
        let registry = new FinalizationRegistry(data => {
            console.log("销毁了", data)
        })
        let wmap = new WeakMap()
        let like = new WeakRef(document.getElementById("like"))
        wmap.set(like.deref(), { click: 0 })
        like.deref().onclick = function () {
            let times = wmap.get(like.deref())
            times.click++
            console.log(times.click)
        }
        setTimeout(() => {
            registry.register(like.deref(), wmap.get(like.deref()).click)
            document.body.removeChild(like.deref())
        }, 2000)
    </script>
</body>

</html>